﻿using System.Linq;
using System.Collections.Generic;
using System;
using System.Security.Principal;

using Csla;
using Csla.Security;

namespace Haushaltsbuch.Domain.Authentication
{
    [Serializable]
    public class HaushaltsbuchPrincipal : CslaPrincipal
    {
        private HaushaltsbuchPrincipal(IIdentity identity) : base(identity) {}

        public static void Login(string username, string password)
        {
            var identity = HaushaltsbuchIdentity.Get(username, password);
            if (!identity.IsAuthenticated)
            {
                Logout();
                return;
            }

            ApplicationContext.User = new HaushaltsbuchPrincipal(identity);

        }

        public static void Load(string username)
        {
            var identity = HaushaltsbuchIdentity.Get(username);
            ApplicationContext.User = new HaushaltsbuchPrincipal(identity);
        }

        public static void Logout()
        {
            ApplicationContext.User = new UnauthenticatedPrincipal();
        }
    }
}